'use strict';
import React, {Component, PropTypes} from 'react';
import { connect } from 'react-redux';
import { Modal} from 'antd';
import { setDialog }   from '../../../reducers/common/actions.js';
import { saveRow }   from '../../../reducers/role/actions.js';
import PermissionBox  from './PermissionBox';

class RoleEditDialog extends Component {
    constructor(props) {
        super(props);
    }
    render() {
        let { row,dialogKey,confirmLoading } = this.props;
        let visible = this.props.dialog[dialogKey] ? true : false;
        let modalTitle =  "修改权限";
        const formItemLayout = {
            labelCol: { span: 3 },
            wrapperCol: { span: 21 },
        };
        let modalProps = {};
        if(row.key){
            modalProps.width = 760;
        }
        return (
            <Modal
                {...modalProps}
                title={modalTitle}
                visible={visible}
                confirmLoading={confirmLoading}
                onOk={this.handleOk.bind(this)}
                onCancel={this.handleCancel.bind(this)} >
                <PermissionBox/>
            </Modal>
        )
    }
    handleOk(){
        let {permission,permissionNodes} = this.props.row;
        let _permission = {};
        for(let key in permission){
            if(permission[key] && permissionNodes[key]){
                _permission[key] = true;
            }
        }
        this.props.saveRow({
            permission:_permission
        },()=>{
            this.props.setDialog(this.props.dialogKey,false);
        })
    }
    handleCancel(e){
        let { confirmLoading } = this.props;
        if( confirmLoading ) return false;
        this.props.setDialog(this.props.dialogKey,false);
    }
}

export default connect(
    state =>{
        return {
            row:state.role.row,
            confirmLoading:state.common.loading,
            dialog: state.common.dialog,
        };
    },
    dispatch => {
        return {
            setDialog:(key,value)=>dispatch(setDialog(key,value)),
            saveRow:(row,cb)=>dispatch(saveRow(row,cb))
        }
    }
)(RoleEditDialog);